<?php

/**
 * Subclass for representing a row from the 'category' table.
 *
 * 
 *
 * @package lib.model
 */ 
class Category extends BaseCategory
{
	protected $m_photoName = "";
	
	public $parent_id;
	
	public function __toString()
	{
		return $this->getName() ? $this->getName() : $this->id;
		
	}
	
	protected static $m_arItems = array();
	
	/**
	 * Retrieves (cached) by ID
	 *
	 * @param integer $id ID of category
	 * @return Category
	 */
	public static function getOrCreate($id)
	{
	    $category = null;
//	    if( empty( self::$m_arItems[$id] ) )
//	    {
	        $category = CategoryPeer::retrieveByPK($id);
	        self::$m_arItems[$id] = $category;
//	    }
	    
	    if( empty($category) )
	    {
	        $category = new Category();
	    }
	    
	    return $category;
	}
	
	/**
	 * Faq::hydrate()
	 * Sets culture to result set retrieved from database in order not to set it everytime when writing new method
	 * @param ResultSet $rs
	 * @param integer $startcol
	 *
	 */
	public function hydrate(ResultSet $rs, $startcol = 1)
	{
		$this->setCulture(sfContext::getInstance()->getUser()->getCulture());
		parent::hydrate($rs, $startcol);
	}

	public function hasPhoto($id)
	{
		// No photo, we checked it
		if( $this->m_photoName == "-" )
		{
			return false;
		}
		
		// Check for photo
		if( $this->m_photoName == "" )
		{
			// Select from DB
			$this->m_photoName = "xxxx";
			
			$this->isPhoto = false;
			$product = ProductPeer::retrieveByPk($id);
			$c = new Criteria();
			$c->add( FilesPeer::OBJ_ID, $id);
			$c->add( FilesPeer::OBJ_TYPE, MyConstants::TYPE_PRODUCT );
			$photo = FilesPeer::doSelectOne($c);
			if( $photo )
			{
				$this->isPhoto = true;
				$this->m_photoName = $photo->getUname();
			}			
		}
		
		return $this->m_photoName != "-";
	}
	
	public function getPhotoName($id)
	{
		if( $this->hasPhoto($id) )
		{
			return $this->m_photoName;
		}
		
		return "";
	}
	
	
	/**
	 * delete category from database
	 * reduce children_qnt to parent category
	 *
	 * @param
	 */
	public function delete($con = null)
	{
//		debugvar($this);
		if($this->parent_id)
		{
			$category_parent = CategoryPeer::retrieveByPk($this->parent_id);
			if( $category_parent )
			{
				if($category_parent->getChildrenQnt())
				{
//			debugvar($category_parent);
//			die('category_parent');

					$category_parent->setChildrenQnt($category_parent->getChildrenQnt() - 1);
					//debugvar($category_parent);
					//exit('saving...');
					$category_parent->save();
				}
			}
		}
		Files::deleteByObjId($this->id, true, MyConstants::TYPE_CATEGORY);
		
		parent::delete($con);
	}

	public function save($con = null)
	{
//		debugvar($this);
//		debugvar('this...');
//		debugvar($this->children_qnt);
//		debugvar($this->branch);
//		debugvar($this->parent_id);
		
		//$this->culture = $this->getUser()->getCulture();
		//$category->setCulture($this->getUser()->getCulture());
		
		// TODO: Check if parent ID is modified
		
		if($this->parent_id)
		{
			if($this->isNew())
			{
				$category_parent = CategoryPeer::retrieveByPk($this->parent_id);
				if( $category_parent )
				{
					$category_parent->setChildrenQnt($category_parent->getChildrenQnt() + 1);
//					debugvar($category_parent);
//					exit('saving...');
					$category_parent->save();
				}
			}
		}
		
		parent::save($con);
	}

	/**
	 * Returns category by id
	 *
	 * @param integer $id
	 * @return Category
	 */

/*	
	public static function getCategoryById($id) 
    {
//    	debugvar($id);
//    	debugvar('categoryById...');
    	
        return CategoryPeer::retrieveByPk($id);
        
//        debugvar($category);
//        debugvar('CategoryPeer...');
//        if($category)
//        	debugvar('yes...');
//        else 
//        	debugvar('no...');
        	
       return $category;
    }
*/

	/**
	 * Returns list of non-deleted categories by their parent ID
	 *
	 * @param integer $parentId ID of parent
	 * @return array of Category objects
	 */
    public static function getList($parentId)
    {
    	$c = new Criteria();
    	$c->add(CategoryPeer::PARENT_ID, (int)$parentId);
    	$c->add(CategoryPeer::STATUS_ID, MyConstants::STATUS_DELETED, Criteria::NOT_EQUAL);
    	return CategoryPeer::doSelect($c);
    }

    public static function getRealList($parentId)
    {
    	$id = $parentId;
    	$c = new Criteria();
    	$c->add(CategoryPeer::PARENT_ID, $parentId);
    	$c->add(CategoryPeer::STATUS_ID, MyConstants::OBJ_STATUS_ACTIVE);
    	return CategoryPeer::doSelect($c);
    }
    
	
	/**
	 * Return array with parents category id
	 *
	 * @param integer $id
	 * @return array
	 */
	public static function getParentsId($parentId)
    {
		$in_parent_id = $parentId;
		$a_parentsId = array();

//debugvar($in_parent_id, 'getParentsId:in_parent_id');

		while($in_parent_id)
		{
			$a_parentsId[] = $in_parent_id;
	    	$category = CategoryPeer::retrieveByPk($in_parent_id);

            if(!$category)
            {
              break;
            }
	    	
            $in_parent_id = $category->parent_id;
		}
    	
    	return array_reverse($a_parentsId);
    }
    
	/**
	 * Return array with parents category branch
	 *
	 * @param integer $id
	 * @return array
	 */
	public static function getBranchById($id)
    {
		$in_parent_id = $id;
		$a_branch = array();

//debugvar($in_parent_id, 'getParentsId:in_parent_id');

		$i = 0;
		while($in_parent_id)
		{
	    	$category = CategoryPeer::retrieveByPk($in_parent_id);
	    	
	    	if(!$category)
	    	{
	    	  break;
	    	}
	    	
	    	//debugvar($category, 'category');
            $a_branch[$i]['id'] = $in_parent_id;
	    	$a_branch[$i]['name'] = $category->getName();

			$in_parent_id = $category->parent_id;
			$i++;
		}
    	//debugvar($a_branch,'a_branch');
    	return array_reverse($a_branch);
    }
    	
	/**
	 * Return array with categories list, category parent_id, selected category
	 *
	 * @param integer $parentId
	 * @return array
	 */
	public static function getListParent($parentId)
    {
//    	debugvar($parentId, 'parentId');

    	$category = CategoryPeer::retrieveByPk($parentId);
    	
    	$c = new Criteria();
    	$c->add(CategoryPeer::PARENT_ID, $parentId);
    	$ar = CategoryPeer::doSelect($c);
    	$categories = array();
    	
    	if( $ar )
    	{
	    	foreach( $ar as $cat )
	    	{
	    		$categories[$cat->getId()] = $cat->getName();
	    		$parent_id = $cat->getParentId();

				if($cat->getId() == $parentId)
	    		{
	    			$selected = $cat->getId();
	    		}
	    	}
    	}
    	else 
    	{
    		//$categories = '';
    	}
    	
		//debugvar($selected, 'selected');
		//return $this->renderText($select);
		
    	return $categories;		
    }
    
}
